package com.beidu.lottery.call.mq.rabbit;


import com.alibaba.fastjson.JSONObject;
import com.beidu.lottery.call.constant.RabbitMQConstant;
import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.entity.dto.ActivityLotteryDTO;
import com.beidu.lottery.call.service.portal.ActivityLotteryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 抽奖活动 消费类
 * </p>
 *
 * @author 北渡
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ActivityLotteryListener {
    private final ActivityLotteryService activityLotteryService;

    /**
     * 抽奖消息监听
     *
     * @param message 数据
     * @author lizhongjun
     */
    @RabbitListener(queues = RabbitMQConstant.LOTTERY_SUBMIT_QUEUE)
    public void handleMsg(String message) {
        long startTime = System.currentTimeMillis();
        log.info("开始消费抽奖数据message => {}", message);
        // 转换对象
        JSONObject data = JSONObject.parseObject(message);
        ActivityLotteryDTO dto = data.getObject("dto", ActivityLotteryDTO.class);// 抽奖数据
        PortalActivityRule rule = data.getObject("rule", PortalActivityRule.class);// 抽奖规则
        // 抽奖数据提交
        activityLotteryService.drawSubmit(dto, rule);
        log.info("抽奖数据消费完成messageId => {}, 耗时：{}毫秒", dto.getMessageId(), System.currentTimeMillis() - startTime);
    }


}
